<?php 
class JobselectCtl extends BaseCtl {
	public function JobselectCtl(){
		parent::BaseCtl();
	}

    public function isPostback(){
    	return 'jobselect' == $this->getPost('pagename');
    }
    
    public function loadData(){
    	$form = $this->getJobselectForm(); 
    	
    	// load the scalar elements (which are not a whole lot of them he he he he he)
    	$form->setAlbumId($this->getPost('albumId'));
    	$form->setJobId($this->getPost('jobId'));
    	$form->setJobName($this->getPost('jobName'));
    	
    	// now let's load the selected stuff 
    	$form->setSelectedPictures($this->getSelected('selectedPictures'));
    	$form->setSelectedJobPictures($this->getSelected('selectedJobPictures'));
    	
    	// albums, pictures and job pictures will not be processed here because
    	// they are not the subject of simple postback
    }
    
		
	public function update(){
		try {
			$form = $this->getJobselectForm();
			// get the selected album
			$albumId = $form->getAlbumId();
			
			// seek for the pictures associated with it and select them
			$service = new Service();
			$pictures = $service->getPicturesByAlbumId($albumId);
			$ids = array();
			
			foreach($pictures as $current){
				$ids[] = $current->getId();
			}
			$form->setPictures($ids);
			
			// reset the selected pictures
			$form->setSelectedPictures(array());
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}
	
	public function select(){
		try {
			$form = $this->getJobselectForm();
			$jobId = $form->getJobId();
			
			$selected = $form->getSelectedPictures();
			
			if(count($selected) == 0){
				throw new Exception('Please select at least one item to add to the current job');
			}
			else {
				$ids = array(); 
				
				foreach($selected as $key => $val){
					$ids[] = $key; 
				}
				
				$service = new Service();
				
				$initial = count($ids);
				$already = $service->getAlreadyJobPictureIds($jobId, $ids);
				
				$toAdd = $this->getToAdd($ids, $already);
				
				$final = count($already); 
				
				if($final == $initial){
					HtmlUtil::addMessage('All the pictures you wanted to add are already part of the job');
				}
				else {
					$service->addJobPictures($jobId, $toAdd);
					$form->addJobPictures($toAdd);
					
					$form->setSelectedPictures(array());
					
					$diff = $initial - $final;
					HtmlUtil::addMessage("Added {$diff} picture(s) from the selected {$initial}");
				}
			}
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}
	
	private function getToAdd($ids, $already){
		$flip = array_flip($already); 
		$arr = array();
		
		foreach($ids as $current){
			if(! array_key_exists($current, $flip)){
				$arr[] = $current;
			}
		}
		
		return $arr; 
	}
	
	public function remove(){
		try {
			$form = $this->getJobselectForm();
			$jobId = $form->getJobId();
			
			$selected = $form->getSelectedJobPictures();
			
			if(count($selected) == 0){
				throw new Exception('Please select at least one item to remove');
			}
			else {
				$ids = array();
				foreach($selected as $key => $val){
					$ids[] = $key;
				}
				
				$service = new Service();
				$service->removeJobPictures($jobId, $ids); 
				
				$form->removeJobPictures($ids);
				$form->setSelectedJobPictures(array());
			}
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}
}

?>